package com.tedu.jsd2407_ivos_fzj.dictoption.service.impl;

import com.tedu.jsd2407_ivos_fzj.base.exception.ServiceException;
import com.tedu.jsd2407_ivos_fzj.base.response.StatusCode;
import com.tedu.jsd2407_ivos_fzj.dict.mapper.DictMapper;
import com.tedu.jsd2407_ivos_fzj.dict.pojo.dto.DictQuery;
import com.tedu.jsd2407_ivos_fzj.dict.pojo.vo.DictVO;
import com.tedu.jsd2407_ivos_fzj.dictoption.mapper.DictOptionMapper;
import com.tedu.jsd2407_ivos_fzj.dictoption.pojo.dto.DictOptionParam;
import com.tedu.jsd2407_ivos_fzj.dictoption.pojo.dto.DictOptionQuery;
import com.tedu.jsd2407_ivos_fzj.dictoption.pojo.entity.DictOption;
import com.tedu.jsd2407_ivos_fzj.dictoption.pojo.vo.DictOptionVO;
import com.tedu.jsd2407_ivos_fzj.dictoption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictOptionServiceImpl implements DictOptionService {
    @Autowired
    private DictOptionMapper dictOptionMapper;
    @Autowired
    private DictMapper dictMapper;

    @Override
    public List<DictOptionVO> select(DictOptionQuery dictOptionQuery) {
        log.debug("字典项查询业务，参数：{}",dictOptionQuery);
        List<DictOptionVO> dictOptionVOList = dictOptionMapper.select(dictOptionQuery);
        return dictOptionVOList;
    }

    @Override
    public void save(DictOptionParam dictOptionParam) {
        log.debug("保存业务，参数：{}",dictOptionParam);
        DictOption dictOption = new DictOption();

        BeanUtils.copyProperties(dictOptionParam,dictOption);
        if(dictOptionParam.getId()==null){
            dictOption.setCreateTime(new Date());
            dictOptionMapper.insert(dictOption);
        }else{
            dictOption.setUpdateTime(new Date());
            dictOptionMapper.update(dictOption);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("根据id删除业务，参数：{}",id);
        dictOptionMapper.deleteById(id);
    }

    @Override
    public List<DictOptionVO> selectByCode(String code) {
        log.debug("根据字典code查询字典项，参数：{}",code);
        List<DictOptionVO> dictOptionVOList = dictOptionMapper.selectByCode(code);
        if(dictOptionVOList!=null&&!dictOptionVOList.isEmpty()){
            log.debug("根据字典code查询字典项，结果：{}",dictOptionVOList);
            return dictOptionVOList;

        }else{
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
    }

    @Override
    public List<DictOptionVO> selectByCode_pro(String code) {

        DictQuery dictQuery = new DictQuery();
        dictQuery.setCode(code);
        List<DictVO> dictVOList = dictMapper.select(dictQuery);
        if(dictVOList!=null&&!dictVOList.isEmpty()){
            DictVO dict = dictVOList.get(0);//字典

            DictOptionQuery dictOptionQuery = new DictOptionQuery();
            dictOptionQuery.setDictId(dict.getId());
            List<DictOptionVO> dictOptionVOList = dictOptionMapper.select(dictOptionQuery);
            return dictOptionVOList;
        }else{
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }


    }

    public void deleteByName(Long id) {
        log.debug("根据id删除业务，参数：{}",id);
        dictOptionMapper.deleteById(id);
    }
}
